
import globalApi from '@/api/global'
import { formatDate, formatArray } from '@/utils'

// 新增、编辑、详情 相关
export const detailsMinin = {
    data() {
        return {
            formData: {},
            moduleData: [],
            checkKeysData: [],  // 用来保存复选框的keys
            pageType: 'create',
            pageParams: null,
        }
    },
    methods: {
        // 取消 操作
        handleCancle(){
            this.$router.go(-1);
        },
        // 提交 操作
        handleSubmit(name,params){
            console.log(this.pageType)
            this.$refs['form'].validate((valid) => {
                if(valid){
                    let formData = JSON.parse(JSON.stringify(this.formData));
                    // if(formData.create_time){
                    //     delete formData.create_time;
                    // }
                    // if(formData.update_time){
                    //     delete formData.update_time;
                    // }
                    if (formData.appoint_time) {
                        formData.appoint_time = formatDate.toStr(new Date(formData.appoint_time),'YYYY-MM-DD HH:mm:ss');
                    }
                    if(formData.start_date){
                        formData.start_date = formatDate.toStr(new Date(formData.start_date),'YYYY-MM-DD');
                    }
                    if(formData.end_date){
                        formData.end_date = formatDate.toStr(new Date(formData.end_date),'YYYY-MM-DD');
                    }
                    if(formData.birthday){
                        formData.birthday = formatDate.toStr(new Date(formData.birthday),'YYYY-MM-DD');
                    }
                    if(this.pageType=='create'){
                        formData.center_id = JSON.parse(sessionStorage.getItem('user')).center_id;
                    }
                    console.log('formData:',formData)
                    if(this.pageType=='create'){
                        this.http.createContent(formData).then(res=>{
                            this.$message.success(res.msg || '新增成功！');
                            this.$router.push({name,params});
                        }).catch(err => {
                          this.$message.success(err.msg || '新增失败！');
                        })
                    } else {
                        this.http.editContent(formData).then(res=>{
                            this.$message.success(res.msg || '编辑成功！');
                            this.$router.push({name,params});
                        }).catch(err => {
                          this.$message.success(err.msg || '编辑失败！');
                        })
                    }
                }
            })    
        },
        // details 组件中更新内容后同步回父组件
        changeFormData(formData){
            this.formData = { ...formData }
        },
        
        getContentDetail(id){
            this.http.getContentDetail({id}).then(res=>{
                let formData = res.data
                if(this.checkKeysData.length){
                    this.checkKeysData.map(item => {
                        if(formData[item]){
                            // console.log(formData[item])
                            // formData[item] = formData[item].split(",");
                        } else {
                            formData[item] = [];
                        }
                    })
                }
                if(formData['headimgurl']){
                    this.imageUrl = formData['headimgurl'];
                }
                this.formData = formData;
                // 级别判断
                let basicConfig = JSON.parse(sessionStorage.getItem('basicConfig'));
                this.levelList = formatArray(basicConfig.user_level);
                if (this.pageParams.pageType === 'edit' || this.pageParams.pageType === 'details') {
                    let startLevel = formData.initial_level
                    let midLevel = formData.current_level
                    let endLevel = formData.target_level
                    for (let level of this.levelList) {
                        if (startLevel === level.label.code) {
                            this.startLevel = Number(level.label.id)
                        }
                        if (midLevel === level.label.code) {
                            this.midLevel = Number(level.label.id)
                        }
                        if (endLevel === level.label.code) {
                            this.endLevel = Number(level.label.id)
                        }
                    }
                    if (this.startLevel <= this.midLevel <= this.endLevel) {
                        this.isSubmit = true
                    }
                }
            })
        },
        getCommonModuleFields(module){
            globalApi.getCommonModuleFields({module}).then(res => {
                let moduleData = res.data;
                if(moduleData.length){
                    moduleData.map(item=>{
                        if(item.data.length){
                            item.data.map(it=>{
                                if(it.type=='checkboxarray'){
                                    this.$set(this.formData, it.value, []);
                                    this.checkKeysData.push(it.value);
                                }
                            })
                        }
                    })
                }
                // console.log('this.formData:',this.formData)
                this.moduleData = moduleData;       // 传入 details 组件中的内容

                if(this.pageParams.id){
                    this.getContentDetail(this.pageParams.id);
                }
            })
        },
        getPageParams(pageParamsTitle,callback){
            if(Object.keys(this.$route.params).length){
                sessionStorage.setItem(pageParamsTitle,JSON.stringify(this.$route.params))
            }
            let pageParams = sessionStorage.getItem(pageParamsTitle);
            if(pageParams){
                pageParams = JSON.parse(pageParams);
                this.pageType = pageParams.pageType;
                this.pageParams = pageParams;
                callback();
            }
        }
    },
    created() {
        this.init();
    },
}